package pathManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class PathManager {
	
	private static PathManager _objectSingleton;
	
	ArrayList<File> filePaths = new ArrayList<File>();

	private PathManager (){}
	
	public static PathManager getManager(){
		if(_objectSingleton == null){
			_objectSingleton = new PathManager();
		}
		return _objectSingleton;
	}
	
	public void setPaths(String[] paths) {
		File file;
		
		for (String str : paths) {
			file = new File(str);
			
			if(isPathOK(file)){
				filePaths.add(file);
			}
			else{System.out.println(file.getName() + " is not valid or is not accesible");}
		}
	}
	
	private boolean isPathOK(File file) {
		return file.canRead() && !file.isDirectory() && file.exists();
	}
	
	public ArrayList<File> getFilePaths() {
		return this.filePaths;
	}
	
	public int getNumberOfFiles(){
		return filePaths.size();
	}
	
	private int getNumberOfLines(File file) throws IOException{
		BufferedReader reader = new BufferedReader(new FileReader(file));
		int lines = 0;
		while (reader.readLine() != null) lines++;
		reader.close();
		
		return lines;
	}
	
	public void printFileInfo(File file) throws IOException{
		System.out.println(file.getName() + " has " + getNumberOfLines(file) + " lines");
	}

	
}
